import os
from openpyxl import load_workbook 
import shutil
import json

def load_config(config_file="config.json"):
    with open(config_file, "r") as file:
        config = json.load(file)
    return config

config = load_config()

# 交易文件存放目录
dir0 = config["dir0"]
# 漏传交易文件 交易时间 终端设备
xls_name = config["xls_name"]
# 找到交易文件后存放目录
dir_lost = config["dir_lost"]

# 通过交易时间找
def excel_files00(dir):
    wb = load_workbook(xls_name)
    sheets = wb.worksheets
    print(sheets)

    sheet1 = sheets[0]
    # print(sheet1[1])
    rows = sheet1.rows
    for row in rows:
        row_val = [col.value for col in row]
        # 取设备交易时间符合文件
        move_files00(dir,row_val[1],row_val[3])

# 找设备交易时间符合文件
def move_files00(dir,trade_time,equ_name):
    for path in os.listdir(dir):
        if equ_name in path :
            # 解析文件
            path_file = dir + '/' + path
            with open(path_file, 'rb') as file:
                content = file.read()
            hex_str = content.hex()
            if trade_time in hex_str:
                print(path)
                print(trade_time)
                print(hex_str)
                if os.path.exists(path_file):
                    shutil.copy(path_file, dir_lost + "/" + path)

excel_files00(dir0)

        
